package com.tiancheng.trade.settlement.controller.admin;


import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.exception.error.ArgumentErrorInfoEnum;
import com.tiancheng.trade.settlement.exception.error.BusinessErrorInfoEnum;
import com.tiancheng.trade.settlement.exception.error.CommonErrorInfoEnum;
import com.tiancheng.trade.settlement.model.settlement.PropertyInfo;
import com.tiancheng.trade.settlement.service.settlement.impl.PropertyInfoService;
import com.tiancheng.trade.settlement.util.DictUtil;
import com.tiancheng.trade.settlement.vo.settlement.admin.PropertyInfoVO;
import com.tiancheng.trade.settlement.vo.settlement.api.KeyValueVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/admin/property")
public class PropertyAdminController {

    @Autowired
    private PropertyInfoService propertyInfoService;

    @GetMapping(value = "/dictionary")
    public Result getDict(@RequestParam(name = "type", required = false) String type) throws BusinessException {
        if (StringUtils.isBlank(type)) {
            Map<String, List<KeyValueVO>> all = DictUtil.getDict();
            return Result.success(all);
        }
        List<KeyValueVO> dictVoList = DictUtil.getDict(type);
        return Result.success(dictVoList);

    }


    @GetMapping("/list")
    public Result queryList(PropertyInfo propertyInfo) throws BusinessException {

        return Result.success(propertyInfoService.queryList(propertyInfo));
    }

    @GetMapping("/getPageList")
    public Result queryPageList(PropertyInfoVO propertyInfo) throws BusinessException {
        PageInfo<PropertyInfo> result = propertyInfoService.queryList(propertyInfo);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    public Result queryList(@PathVariable("id") Long id) throws BusinessException {
        if (id == null) {
            return Result.fail(ArgumentErrorInfoEnum.VALID_ERROR.getCode(), ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        }
        return Result.success(propertyInfoService.query(id));

    }

    @PostMapping("/")
    public Result create(@RequestBody PropertyInfo propertyInfo) throws BusinessException {
        if (propertyInfo == null) {
            return Result.fail(ArgumentErrorInfoEnum.VALID_ERROR.getCode(), ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        }
        PropertyInfo data = propertyInfoService.create(propertyInfo);

        if (data == null) {
            return Result.fail(BusinessErrorInfoEnum.ADD_REMARK_ERROR.getCode(), BusinessErrorInfoEnum.ADD_REMARK_ERROR.getMessage());
        }
        return Result.success(data);

    }

    @PostMapping("/batch")
    public Result save(@RequestBody List<PropertyInfo> list) throws BusinessException {
        if (list == null || list.isEmpty()) {
            return Result.fail(ArgumentErrorInfoEnum.VALID_ERROR.getCode(), ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        }
        for (PropertyInfo propertyInfo : list) {
            if (propertyInfo.getId() == null) {
                PropertyInfo data = propertyInfoService.create(propertyInfo);
            } else {
                int cnt = propertyInfoService.update(propertyInfo);
            }
        }
        return Result.success(CommonErrorInfoEnum.OPER_SUCCESS.getMessage());

    }

    @PutMapping("/")
    public Result update(@RequestBody PropertyInfo propertyInfo) throws BusinessException {
        if (propertyInfo == null) {
            return Result.fail(ArgumentErrorInfoEnum.VALID_ERROR.getCode(), ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        }
        int ret = propertyInfoService.update(propertyInfo);
        if (ret > 0) {
            return Result.success(ret);
        }
        return Result.fail(CommonErrorInfoEnum.OPER_FAIL.getCode(), CommonErrorInfoEnum.OPER_FAIL.getMessage());
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Long id) throws BusinessException {
        if (id == null) {
            return Result.fail(ArgumentErrorInfoEnum.VALID_ERROR.getCode(), ArgumentErrorInfoEnum.VALID_ERROR.getMessage());
        }

        int ret = propertyInfoService.remove(id);
        if (ret > 0) {
            return Result.success(ret);
        }
        return Result.fail(CommonErrorInfoEnum.OPER_FAIL.getCode(), CommonErrorInfoEnum.OPER_FAIL.getMessage());
    }
}
